package com.example.easycharge.errorHandler;

import com.example.easycharge.error.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/*
    控制器全局错误管理
 */
@RestControllerAdvice
public class ControllerErrorHandler {

    @ExceptionHandler(UsedUserNameError.class)
    @ResponseStatus(HttpStatus.CONFLICT)
    public ErrorResponse usedUserNameError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40000);
        errorResponse.setErrorMessage("用户名已被占用");
        return errorResponse;
    }

    @ExceptionHandler(VehicleNotFoundError.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorResponse vehicleNotFound() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40010);
        errorResponse.setErrorMessage("未查询到此车辆");
        return errorResponse;
    }

    @ExceptionHandler(PageNumError.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse pageNumError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40020);
        errorResponse.setErrorMessage("分页数据错误");
        return errorResponse;
    }

    @ExceptionHandler(OrderByParaError.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse orderByParaError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40030);
        errorResponse.setErrorMessage("排序参数不存在");
        return errorResponse;
    }

    // 新增一个UserNotFound
    @ExceptionHandler(UserNotFoundError.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorResponse userNotFoundError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40040);
        errorResponse.setErrorMessage("未查询到此用户");
        return errorResponse;
    }

    @ExceptionHandler(MessageNotFoundError.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorResponse messageNotFoundError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40050);
        errorResponse.setErrorMessage("消息不存在");
        return errorResponse;
    }

    @ExceptionHandler(AttributeNotFoundError.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse attributeNotFoundError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40060);
        errorResponse.setErrorMessage("用户不存在此属性字段");
        return errorResponse;
    }

    @ExceptionHandler(CanNotCancelError.class)
    @ResponseStatus(HttpStatus.LOCKED)
    public ErrorResponse canNotCancelError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40070);
        errorResponse.setErrorMessage("无法取消充电，请查看车辆状态");
        return errorResponse;
    }

    @ExceptionHandler(CanNotStartChargeError.class)
    @ResponseStatus(HttpStatus.LOCKED)
    public ErrorResponse canNotStartChargeError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40080);
        errorResponse.setErrorMessage("无法开始充电，请查看车辆状态");
        return errorResponse;
    }

    @ExceptionHandler(DriveAwayError.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse driveAwayError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40090);
        errorResponse.setErrorMessage("车辆驶离不合规");
        return errorResponse;
    }

    @ExceptionHandler(QueueNotFoundError.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorResponse queueNotFoundError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40110);
        errorResponse.setErrorMessage("未查询到此队列");
        return errorResponse;
    }

    @ExceptionHandler(QueueExistError.class)
    @ResponseStatus(HttpStatus.CONFLICT)
    public ErrorResponse queueExistError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40120);
        errorResponse.setErrorMessage("此充电队列已存在");
        return errorResponse;
    }

    @ExceptionHandler(RecordNotFoundError.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorResponse recordNotFoundError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40160);
        errorResponse.setErrorMessage("此记录不存在");
        return errorResponse;
    }

    @ExceptionHandler(PileNotFoundError.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorResponse pileNotFoundError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40150);
        errorResponse.setErrorMessage("此充电桩不存在");
        return errorResponse;
    }

    @ExceptionHandler(AlertNotFoundError.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorResponse alertNotFoundError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(40130);
        errorResponse.setErrorMessage("此警报不存在");
        return errorResponse;
    }

    @ExceptionHandler(InvalidOperateError.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse invalidOperateError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(400140);
        errorResponse.setErrorMessage("无效操作");
        return errorResponse;
    }

    @ExceptionHandler(ReChargingError.class)
    @ResponseStatus(HttpStatus.CONFLICT)
    public ErrorResponse reChargingError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(400150);
        errorResponse.setErrorMessage("已充电车辆重复申请充电");
        return errorResponse;
    }

    @ExceptionHandler(WithoutAuthorityError.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ErrorResponse withoutAuthorityError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(400160);
        errorResponse.setErrorMessage("没有操作权限");
        return errorResponse;
    }

    @ExceptionHandler(BillNotFoundError.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorResponse billNotFoundError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(400150);
        errorResponse.setErrorMessage("未查询到此账单");
        return errorResponse;
    }

    @ExceptionHandler(ServiceNotSupport.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse serviceNotSupport() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(400160);
        errorResponse.setErrorMessage("服务无法提供");
        return errorResponse;
    }

    @ExceptionHandler(PileStillWordError.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse pileStillWordError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(400170);
        errorResponse.setErrorMessage("无法删除正在工作的充电桩");
        return errorResponse;
    }

    @ExceptionHandler(VehicleNotWorkError.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ErrorResponse vehicleNotWorkError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(400180);
        errorResponse.setErrorMessage("队列中不存在此车辆");
        return errorResponse;
    }

    @ExceptionHandler(OverChargeError.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ErrorResponse overChargeError() {
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrorCode(400190);
        errorResponse.setErrorMessage("充电电量数据错误");
        return errorResponse;
    }
}
